package J1206_1208;

public class BinaryTree {
     public static class TreeNode {
         char val;
         TreeNode left;
         TreeNode right;
         public TreeNode(char val) {
             this.val = val;
         }
     }
     //先利用穷举法 创建一颗二叉树
    public TreeNode craetTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;

        B.left = D;
        B.right = E;

        C.left = F;
        C.right = G;

        E.right = H;
        return A;
    }

    //先序遍历
    public void preOrder(TreeNode root) {
         if(root == null) {
             return; //空树不需要遍历
         }
         //先序遍历 ： 根 左 右
         System.out.print(root.val+" ");
         preOrder(root.left);
         preOrder(root.right);
    }

    //中序遍历 ： 左 根 右
    public void inOrder(TreeNode root) {
         if(root == null) {
             return;
         }
         inOrder(root.left);
         System.out.print(root.val+" ");
         inOrder(root.right);
    }

    //后序遍历 ： 左 右 根
    public void postOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        inOrder(root.left);
        inOrder(root.right);
        System.out.print(root.val + " ");
    }

    public static int size = 0;
    public void size(TreeNode root) {
        if(root == null) {
            return;
        }
        size++;
        size(root.left);
        size(root.right);

    }
    public int leafsize = 0;
    public void LeafSize1(TreeNode root) {
        if(root == null) {
            return;
        }
         if(root.left == null && root.right == null) {
             leafsize++;
             return;
         }
         LeafSize1(root.left);
         LeafSize1(root.right);
    }
    public int LeafSize2(TreeNode root) {
        if(root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            return 1;
        }
        return LeafSize2(root.left)+LeafSize2(root.right);
    }

    //计算某一层有几个结点
    public int getLevelNodeCount(TreeNode root,int k) {
        if(root == null) {
            return 0;
        }
        if(k == 1) {
            return 1;
        }

        return getLevelNodeCount(root.left,k-1)
                +getLevelNodeCount(root.right,k-1);
    }

    //获取二叉树的高度
    public int getHight(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int left = getHight(root.left);
        int right = getHight(root.right);
        return Math.max(left,right) + 1;
        //return ((left > right)? left : right) + 1;
    }

    //在一棵树中查找结点val
    public TreeNode find(TreeNode root,int val) {
        if (root == null) {
            return null;
        }
        if(root.val == val) {
            return root;
        }
        TreeNode left = find(root.left,val);
        if(left != null) {
            return left;
        }
        TreeNode right = find(root.right,val);
        if(right != null) {
            return right;
        }

        return null;
    }

    //检查两棵树是否相同
    //二者结构相同 数据相同 空间复杂度为O（1）
    public  boolean isSameTree(TreeNode A,TreeNode B) {
        if(A != null && B == null || A == null && B != null) {
            return false;
        }

        if(A == null && B == null) {
            return true;
        }

        if(A.val != B.val) {
            return false;
        }

        return isSameTree(A.left,B.left) && isSameTree(A.right,B.right);
    }

    //判断一颗树中有没有包含另一棵树
    //先判断根节点是不是相同的树 再判断左右树是不是相同
    public boolean isSubTree(TreeNode A,TreeNode B) {
        if(A == null) {
            return false;
        }
        if(isSameTree(A,B)) {
            return true;
        }
        if(isSameTree(A.left,B)) {
            return true;
        }
        if(isSameTree(A.right,B)) {
            return true;
        }
        return false;
    }

    //交换两个左右子树
    public TreeNode change(TreeNode root) {
        if(root == null) {
            return null;
        }
        //如果root的左右子树都为空 相当于叶子节点 就不再交换他的左右子树了
        if(root.left == null && root.right == null) {
            return root;
        }
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;
        change(root.left);
        change(root.right);
        return root;
    }

}
